package oj.swing.server;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import oj.jboss.bean.SBean;
import oj.swing.common.DataSTableField;
import oj.swing.common.OJField;
import oj.swing.common.OJTabField;
import oj.util.common.OJEntry;
import oj.util.sUtil.HUtil;

public class OJSwingUtilsBean extends SBean {

	public ArrayList nameMenues() throws RemoteException, SQLException,
			Exception {

		ArrayList menus = new ArrayList();
		Connection con = null;
		OJEntry entry = null;
		try {
			/** Instancio la coneccion */
			con = getConnection();

			String sql = "select name, validation_class,ID_SEARCH from s_window";

			PreparedStatement datos = con.prepareStatement(sql);
			ResultSet rs = datos.executeQuery();
			int pos = 0;

			try {
				while (rs.next()) {

					entry = new OJEntry(HUtil.setStringNoNulo(rs
							.getString("name")), HUtil.setStringNoNulo(rs
							.getString("validation_class")), HUtil
							.setStringNoNulo(rs.getString("ID_SEARCH")));
					menus.add(pos, entry);
					pos++;

				}

			} catch (Exception ex1) {
				throw new Exception(ex1.getMessage());
			}

			finally {
				rs.getStatement().close();
				rs.close();
			}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			con.close();
		}
		return menus;
	}

	public ArrayList ojComponentes() throws RemoteException, SQLException,
			Exception {

		ArrayList menus = new ArrayList();
		Connection con = null;
		DataSTableField entry = null;
		try {
			/** Instancio la coneccion */
			con = getConnection();
			
			String sql = " select * from s_tab_field "
					+ " where id_Table = '23229390061300000316' "
					+ " and is_deleted='N'";

			PreparedStatement datos = con.prepareStatement(sql);
			ResultSet rs = datos.executeQuery();
			int pos = 0;
			OJTabField tabField = null;
			try {
				while (rs.next()) {
					tabField = new OJTabField();

					// IS_VISIBLE
					// VALUE_DEFAULT
					// IS_UPPER_CASE

					if (rs.getString("CONTINUE_PREV").equals("Y"))
						tabField.setContinuePrev(true);
					else
						tabField.setContinuePrev(false);

					tabField.setIdField(rs.getString("ID_TAB_FIELD"));
					tabField.setIdTable(rs.getString("ID_TABLE_FIELD"));
					tabField.setId(rs.getString("ID_TAB"));
					tabField.setLabelSize(rs.getBigDecimal("LABEL_SIZE")
							.intValue());
					tabField.setLength(rs.getBigDecimal("LOGICAL_LENGTH")
							.intValue());
					if (rs.getString("IS_MANDATORY").equals("Y"))
						tabField.setMandatory(true);
					else
						tabField.setMandatory(false);

					tabField.setPosition(rs.getBigDecimal("LOGICAL_POSITION")
							.intValue());

					if (rs.getString("READ_ONLY").equals("Y"))
						tabField.setReadOnly(true);
					else
						tabField.setReadOnly(false);

					if (rs.getString("IS_UPPER_CASE").equals("Y"))
						tabField.setUpperCase(true);
					else
						tabField.setUpperCase(false);

					tabField.setValueDefault(rs.getString("VALUE_DEFAULT"));

					if (rs.getString("IS_VISIBLE").equals("Y"))
						tabField.setVisible(true);
					else
						tabField.setVisible(false);

					// entry = new DataSTableField();
					// entry.setDESCRIPTION(rs.getString("DESCRIPTION"));
					// entry.setVISUAL_TYPE(rs.getString("VISUAL_TYPE"));

					OJField field = getField(tabField.getIdTable(), con);

					tabField.setField(field);

					menus.add(pos, tabField);
					pos++;

				}

			} catch (Exception ex1) {
				ex1.printStackTrace();
				throw ex1;
			}

			finally {
				rs.getStatement().close();
				rs.close();
			}

		} catch (Exception e) {
			throw e;
		} finally {
			con.close();
		}
		return menus;
	}

	private OJField getField(String idField, Connection con) throws Exception {

		OJField field = null;
		HUtil hUtil = new HUtil();

		try {

			String sql = " select tf.*, description "
					+ " FROM s_table_field tf "
					+ " inner join s_table_field_lang tfl  on (tf.id_table_field = tfl.id_table_field) "
					+ " where tf.id_Table_field = getIdField ";

			sql = sql.replaceAll("getIdField", hUtil.replParam(idField));
			System.out.println(sql);

			PreparedStatement datos = con.prepareStatement(sql);
			ResultSet rs = datos.executeQuery();

			try {
				while (rs.next()) {

					field = new OJField();

					field.setDataType(rs.getString("DATA_TYPE"));
					field.setDecimalLength((HUtil.setBigdecimalNoNulo(rs
							.getBigDecimal("DECIMAL_LENGTH"), 0).intValue()));
					field.setDefaultType(HUtil.setStringNoNulo(
							rs.getString("DEFAULT_TYPE")).charAt(0));
					field.setDefaultValue(rs.getString("DEFAULT_VALUE"));
					field.setHintCode(rs.getString("ID_HINT"));
					field.setId(rs.getString("ID_TABLE_FIELD"));
					field.setIdSearch(rs.getString("ID_SEARCH"));
					field.setLabel(rs.getString("description"));
					field.setLength((HUtil.setBigdecimalNoNulo(rs
							.getBigDecimal("PHYSICAL_LENGTH"), 0).intValue()));
					// field.setListValues(rs.getString("ID_LIST"));
					if (HUtil.setStringNoNulo(rs.getString("IS_LOGICAL"))
							.equals("Y"))
						field.setLogical(true);
					else
						field.setLogical(false);

					field.setMask(rs.getString("MASK"));

					if (HUtil.setStringNoNulo(rs.getString("IS_NOT_NULL"))
							.equals("Y"))
						field.setNotNull(true);
					else
						field.setNotNull(false);
					field.setPhysicalField(rs.getString("PHYSICAL_FIELD"));
					field.setPickField(rs.getString("PICK_FIELD_PHYS_NAME"));
					field.setPickQuery(rs.getString("PICK_QUERY"));
					field.setPickTable(rs.getString("PICK_TABLE_PHYS_NAME"));

					if (HUtil.setStringNoNulo(rs.getString("IS_PRIMARY_KEY"))
							.equals("Y"))
						field.setPrimaryKey(true);
					else
						field.setPrimaryKey(false);
					field.setSearchDescription(rs
							.getString("SEARCH_DESCRIPTION"));
					field.setTotalPhysField(rs.getString("TOTAL_PHYS_FIELD"));
					field.setVisualType(HUtil.setStringNoNulo(
							rs.getString("VISUAL_TYPE")).charAt(0));
					field.setValidationType(HUtil.setStringNoNulo(
							rs.getString("VALIDATION_TYPE")).charAt(0));
					field.setValidationTo(rs.getString("VALIDATION_TO"));
					field
							.setValidationMethod(rs
									.getString("VALIDATION_METHOD"));
					field.setValidationFrom(rs.getString("VALIDATION_FROM"));
					if (HUtil.setStringNoNulo(rs.getString("UNIQUE_FIELD"))
							.equals("Y"))
						field.setUniqueField(true);
					else
						field.setUniqueField(false);
					field.setTotalPhysFieldGroup(rs
							.getString("TOTAL_PHYS_FIELD_GROUP"));
					field.setTotalType(HUtil.setStringNoNulo(
							rs.getString("TOTAL_CALC_TYPE")).charAt(0));

				}

			} catch (Exception ex1) {
				throw ex1;
			}

			finally {
				rs.getStatement().close();
				rs.close();
			}
		} catch (Exception e) {
			throw e;
		}

		return field;
	}
}
